// Package models
// @Title  CarouselModel.go
// @Description  轮播图模型
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)

package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

// CarouselObj 用户角色 实体类
type CarouselObj struct {
	Id                 int    `valid:"id"`
	Title                string `valid:"title"`
	ImageUrl               string `valid:"image_url"`
	ArticleId			int `valid:"article_id"`
	Url               string `valid:"url"`
	Status				int `valid:"status"`
	IsDelete				int `valid:"is_delete"`
	UpdateTime                time.Time `valid:"update_time" orm:"auto_now"`
	AddTime                time.Time `valid:"add_time" orm:"auto_now_add"`
}

func (this *CarouselObj) TableName() string {
	return "carousel"
}

func init() {
	orm.RegisterModel(new(CarouselObj))
}

// GetCarouselList 获取分页数据
func GetCarouselList(pageSize, page int) (*[]CarouselObj, int, error) {
	var (
		list []CarouselObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(CarouselObj)).OrderBy("-add_time")

	_, err = qs.Filter("is_delete",0).Limit(pageSize).Offset((page - 1) * pageSize).RelatedSel().All(&list)
	count, _ := qs.Filter("is_delete",0).Count()
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, 0, err
	}

	return &list, int(count), nil
}

// GetCarouseInfoById 根据ID获取记录
func GetCarouseInfoById(id int) (CarouselObj,error) {
	var (
		row CarouselObj
		err  error
	)

	err = orm.NewOrm().QueryTable(new(CarouselObj)).Filter("id", id).Filter("is_delete", 0).One(&row)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return row,nil
	}

	return row,nil
}

// GetUseCarouselList 获取启用的轮播图记录
func GetUseCarouselList() (*[]CarouselObj,error) {
	var (
		list []CarouselObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(CarouselObj)).OrderBy("-add_time")

	_, err = qs.Filter("status",0).Filter("is_delete",0).Limit(10).Offset(0).RelatedSel().All(&list)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, err
	}

	return &list, nil
}

// AddCarouse 添加轮播图
func AddCarouse(title string,imageUrl string,url string,articleId int,status int) int64 {
	carousel := CarouselObj{
		Title:    title,
		ImageUrl: imageUrl,
		Url: url,
		ArticleId: articleId,
		Status: status,
		IsDelete: 0,
	}

	id,err := orm.NewOrm().Insert(&carousel)

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return id
}

// UpdateCarouseById 修改轮播图
func UpdateCarouseById(id int,title string,imageUrl string,url string,articleId int,status int) int64 {
	carousel := CarouselObj{
		Id:    id,
		Title:    title,
		ImageUrl: imageUrl,
		Url: url,
		ArticleId: articleId,
		Status: status,
	}
	num,err := orm.NewOrm().Update(&carousel,"title","image_url","url","status","article_id")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}

// DeleteCarouseById 删除轮播图
func DeleteCarouseById(id int) int64 {
	carouse := CarouselObj{
		Id:    id,
		IsDelete: 1,
	}
	num,err := orm.NewOrm().Update(&carouse,"is_delete")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}